package com.sttg.csp.registration.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.sttg.csp.common.exception.BizException;
import com.sttg.csp.common.service.LoginService;
import com.sttg.csp.registration.dao.CerAssocManageDAO;
import com.sttg.csp.registration.dao.LogManageDAO;

public class CerAssocManageService {

	/**
	 * Logger for this class
	 */
	private final Log logger = LogFactory.getLog(LoginService.class);

	private static final CerAssocManageService instance = new CerAssocManageService();    

	private CerAssocManageDAO cerAssocManageDAO = CerAssocManageDAO.getInstance();
	
	private LogManageDAO logManageDAO = LogManageDAO.getInstance();
	/** 
	 * Private the default constructor.
	 */   
	private CerAssocManageService() {}    
   
	/** 
	 * Static factory methods.
	 */   
	public static CerAssocManageService getInstance() {
		return instance;    
	}    

	public Map queryObject(Map param) throws BizException {
		String uid = (String) param.get("CN");
		if (uid == null || "".equals(uid)) {
			throw new BizException("CN of BD certificate association record can't null");
		}
		return cerAssocManageDAO.queryBDUserReg(param);
	}
	
	public List queryList(Map param) throws BizException {
		return cerAssocManageDAO.queryBDUserRegList(param);
	}
	
	public void doActivate(Map param) throws BizException {
		cerAssocManageDAO.doActivate(param);
		
		Map map = cerAssocManageDAO.queryBDUserReg(param);
		String actionBy = (String) param.get("SESSION_UID");
		String actionFor = (String) map.get("BD_UID");
		Map<String, String> log = new HashMap<String, String>();
		log.put("ACTION_TYPE", "Activate");
		log.put("ACTION_BY", actionBy);
		log.put("ACTION_FOR", actionFor);
		logManageDAO.doInsertLog(log);
	}
	
	public void doDeactivate(Map param) throws BizException {
		cerAssocManageDAO.doDeactivate(param);
		
		Map map = cerAssocManageDAO.queryBDUserReg(param);
		String actionBy = (String) param.get("SESSION_UID");
		String actionFor = (String) map.get("BD_UID");
		Map<String, String> log = new HashMap<String, String>();
		log.put("ACTION_TYPE", "Deactivate");
		log.put("ACTION_BY", actionBy);
		log.put("ACTION_FOR", actionFor);
		logManageDAO.doInsertLog(log);
	}
	
	public void doDelete(Map param) throws BizException {
		cerAssocManageDAO.doDelete(param);
		
		String actionBy = (String) param.get("SESSION_UID");
		String actionFor = (String) param.get("BD_UID");
		Map<String, String> log = new HashMap<String, String>();
		log.put("ACTION_TYPE", "Delete");
		log.put("ACTION_BY", actionBy);
		log.put("ACTION_FOR", actionFor);
		logManageDAO.doInsertLog(log);
	}
	
	public void doPreReg(Map param) throws BizException {
		cerAssocManageDAO.doPreReg(param);
		
		String actionBy = (String) param.get("SESSION_UID");
		String actionFor = (String) param.get("BD_UID");
		Map<String, String> log = new HashMap<String, String>();
		log.put("ACTION_TYPE", "PreReg");
		log.put("ACTION_BY", actionBy);
		log.put("ACTION_FOR", actionFor);
		logManageDAO.doInsertLog(log);
	}
	
	/**
	 * @param param will modify the B/D User self-registration info.
	 * @throws BizException
	 */
	public void doUpdate(Map param) throws BizException {
		cerAssocManageDAO.doUpdateBDUserReg(param);
	}
}
